package com.orm.analysis.service.impl;

import com.orm.analysis.dao.impl.ItemDAO;
import com.orm.analysis.dao.impl.SupplierDAO;
import com.orm.analysis.model.*;
import com.orm.analysis.service.GenericService;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 10/13/13
 * Time: 10:29 AM
 * To change this template use File | Settings | File Templates.
 */
public class ItemService   implements GenericService<Item> {


    private static Logger log = Logger.getLogger(ItemService.class);

    private String itemDataFielDir;

    private ItemDAO dao;

    private SupplierDAO supplierDAO;


    public ItemDAO getDao() {
        return dao;
    }

    public void setDao(ItemDAO dao) {
        this.dao = dao;
    }

    public SupplierDAO getSupplierDAO() {
        return supplierDAO;
    }

    public void setSupplierDAO(SupplierDAO supplierDAO) {
        this.supplierDAO = supplierDAO;
    }

    public String getItemDataFielDir() {
        return itemDataFielDir;
    }

    public void setItemDataFielDir(String itemDataFielDir) {
        this.itemDataFielDir = itemDataFielDir;
    }

    public Item create(Item entity) {
        dao.save(entity);
        return entity;
    }


    public void delete(Object id) {

        dao.delete(id);
    }

    public Item update(Item entity) {
        dao.update(entity);
             return entity;
         }

    public List<Item> list(int page, int size) {
        return dao.list(page,size);
            }

    public List<Item> listByFields(String... fieldsNamesValues) {
        StringBuffer queryString= new StringBuffer("select a from Item a ");
            return dao.listByFields(queryString,fieldsNamesValues);

    }

    public List<Item> findAllWithDepth(String... fetchRelations) {
             return dao.findAllWithDepth(Item.class,fetchRelations);
         }

         public Item findWithDepth(String pkFieldName, Object id, String... fetchRelations) {
             return dao.findWithDepth(Item.class,pkFieldName,id,fetchRelations);
         }
    public  int updateExecuteUpdateByQuery(String query){
        return dao.updateExecuteUpdateByQuery(query);
    }

    public Item getById(Item entity) {
        return dao.getById(entity.getItemId());
      }

    public Item getById(Integer id) {
        return dao.getById(id);
     }


    public void executeImproter() throws ParseException, IOException {
        log.info("File  >>" + itemDataFielDir);
        BufferedReader br = null;

        br = new BufferedReader(new FileReader(itemDataFielDir));

        String line = br.readLine();
        line = br.readLine();

        while (line != null) {
            // ITEM_ID not  need ,ITEM_NAME , ITEM_DESCRIPTION ,QUNTITY_IN_STOCK ,PRICE ,<Supplier>
            Item  item = new Item();

            String[] data = line.split("[,]");

            item.setItemName(data[0]);
            item.setItemDescription(data[1]);
            item.setQuantityInStock(Integer.valueOf(data[2].trim()));
            item.setPrice(Double.valueOf(data[3].trim()));

            dao.save(item);

            if (data.length > 4 && data[4] != null && data[4].length() > 0) {
                List<Supplier> supplierList = new ArrayList<Supplier>();
                String[] supplierIdArray = data[4].split("[~]");
                for (int i = 0; i < supplierIdArray.length; i++) {
                    System.out.println("File  >>"  + supplierIdArray[i]);
                    Supplier supplier =supplierDAO.getById(Integer.valueOf(supplierIdArray[i])); //new Supplier(Integer.valueOf(supplierIdArray[i]));
                    supplier.getItems().add(item);

                    item.getSuppliers().add(supplier);

//                    supplierList.add(supplierDAO.getById(Integer.valueOf(supplierIdArray[i])));
//                    item.getSuppliers().add(new Supplier(Integer.valueOf(supplierIdArray[i])));
                }
//                if (supplierIdArray.length > 0) {
//                    item.getSuppliers().addAll(supplierList);
//                }
            }

            dao.update(item);


            line = br.readLine();

        }

        br.close();

    }
    public List<Integer> getIdList(){

           return dao.findIdList();
    }


    public void processTaskExecutor() {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    public List<Item> getByIdList(Integer[] entityIdList) {
        return dao.findByIdList(entityIdList);
    }
}
